package com.gedeshidai.shumadevelop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gedeshidai.shumadevelop.mapper.ReportMapper;
import com.gedeshidai.shumadevelop.mapper.UserMapper;
import com.gedeshidai.shumadevelop.model.dto.product.GoodsSalesDTO;
import com.gedeshidai.shumadevelop.model.entity.Product;
import com.gedeshidai.shumadevelop.model.entity.User;
import com.gedeshidai.shumadevelop.model.vo.NumberListReportVO;
import com.gedeshidai.shumadevelop.model.vo.ProductPreviewVO;
import com.gedeshidai.shumadevelop.model.vo.SalesTop20ReportVO;
import com.gedeshidai.shumadevelop.model.vo.TurnoverReportVO;
import com.gedeshidai.shumadevelop.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl extends ServiceImpl<UserMapper, User> implements ReportService {
    @Resource
    private ReportMapper reportMapper;

    /**
     * 总销量统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内的每天的日期
        List<LocalDate> dateList=new ArrayList<>();

        List<Integer> turnoverList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            //日期计算
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate date : dateList) {
            //查询date日期对应的当天总销量
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //select sum(category_sales) from product_categories where sell_time>beginTime and sell_time< endTime
            Map map=new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            Integer turnover=reportMapper.sumByMap(map);
            turnover=turnover==null?0:turnover;
            turnoverList.add(turnover);

        }
        //封装返回结果
        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }


    /**
     * Top20
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop20ReportVO getSalesTop20(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MIN);
        Map map=new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        List<GoodsSalesDTO> salesTop20 = reportMapper.getSalesTop20(map);
        List<String> categorys = salesTop20.stream().map(GoodsSalesDTO::getCategory).collect(Collectors.toList());
        String categoryList = StringUtils.join(categorys, ",");

        List<String> sales = salesTop20.stream().map(GoodsSalesDTO::getSales).collect(Collectors.toList());
        String salesList = StringUtils.join(sales, ",");

        //封装返回结果数据
        return SalesTop20ReportVO
                .builder()
                .categoryList(categoryList)
                .salesList(salesList)
                .build();
    }


    /**
     * 兑换方式的数量
     * @param begin
     * @param end
     * @return
     */
    @Override
    public NumberListReportVO getNumberList(LocalDate begin, LocalDate end) {

        List<Integer> NumberList=new ArrayList<>();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MIN);

        Map map=new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        Integer type1 = reportMapper.getNumberType1(map);
        Integer type2 = reportMapper.getNumberType2(map);
        Integer type3 = reportMapper.getNumberType3(map);

        NumberList.add(type1);
        NumberList.add(type2);
        NumberList.add(type3);

        return NumberListReportVO
                .builder()
                .numberList(StringUtils.join(NumberList,","))
                .build();
    }
}
